package org.apache.harmony.javax.security.sasl;

import java.security.Provider;
import java.security.Security;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import org.apache.harmony.javax.security.auth.callback.CallbackHandler;

public class Sasl
{
  private static final String CLIENTFACTORYSRV = "SaslClientFactory";
  public static final String MAX_BUFFER = "javax.security.sasl.maxbuffer";
  public static final String POLICY_FORWARD_SECRECY = "javax.security.sasl.policy.forward";
  public static final String POLICY_NOACTIVE = "javax.security.sasl.policy.noactive";
  public static final String POLICY_NOANONYMOUS = "javax.security.sasl.policy.noanonymous";
  public static final String POLICY_NODICTIONARY = "javax.security.sasl.policy.nodictionary";
  public static final String POLICY_NOPLAINTEXT = "javax.security.sasl.policy.noplaintext";
  public static final String POLICY_PASS_CREDENTIALS = "javax.security.sasl.policy.credentials";
  public static final String QOP = "javax.security.sasl.qop";
  public static final String RAW_SEND_SIZE = "javax.security.sasl.rawsendsize";
  public static final String REUSE = "javax.security.sasl.reuse";
  private static final String SERVERFACTORYSRV = "SaslServerFactory";
  public static final String SERVER_AUTH = "javax.security.sasl.server.authentication";
  public static final String STRENGTH = "javax.security.sasl.strength";

  public static SaslClient createSaslClient(String[] paramArrayOfString, String paramString1, String paramString2, String paramString3, Map<String, ?> paramMap, CallbackHandler paramCallbackHandler)
    throws SaslException
  {
    String[] arrayOfString;
    int i;
    Object localObject;
    int j;
    if (paramArrayOfString == null)
      throw new NullPointerException("auth.33");
    Collection localCollection = findFactories("SaslClientFactory");
    if (localCollection.isEmpty())
      localObject = null;
    while (true)
    {
      SaslClient localSaslClient;
      return localObject;
      Iterator localIterator = localCollection.iterator();
      do
      {
        SaslClientFactory localSaslClientFactory;
        do
        {
          while (!(localIterator.hasNext()))
            localObject = null;
          localSaslClientFactory = (SaslClientFactory)localIterator.next();
          arrayOfString = localSaslClientFactory.getMechanismNames(null);
          i = 0;
          if (arrayOfString != null)
          {
            j = 0;
            if (j < arrayOfString.length)
              break label137;
          }
        }
        while (i == 0);
        localSaslClient = localSaslClientFactory.createSaslClient(paramArrayOfString, paramString1, paramString2, paramString3, paramMap, paramCallbackHandler);
      }
      while (localSaslClient == null);
      localObject = localSaslClient;
    }
    label137: int k = 0;
    while (true)
    {
      if (k >= paramArrayOfString.length);
      while (true)
      {
        while (true)
          ++j;
        if (!(arrayOfString[j].equals(paramArrayOfString[k])))
          break;
        i = 1;
      }
      ++k;
    }
  }

  public static SaslServer createSaslServer(String paramString1, String paramString2, String paramString3, Map<String, ?> paramMap, CallbackHandler paramCallbackHandler)
    throws SaslException
  {
    Iterator localIterator;
    Object localObject;
    int j;
    if (paramString1 == null)
      throw new NullPointerException("auth.32");
    Collection localCollection = findFactories("SaslServerFactory");
    if (localCollection.isEmpty())
      localObject = null;
    while (true)
    {
      return localObject;
      localIterator = localCollection.iterator();
      if (localIterator.hasNext())
        break;
      localObject = null;
    }
    SaslServerFactory localSaslServerFactory = (SaslServerFactory)localIterator.next();
    String[] arrayOfString = localSaslServerFactory.getMechanismNames(null);
    int i = 0;
    if (arrayOfString != null)
      j = 0;
    while (true)
    {
      if (j >= arrayOfString.length);
      while (true)
      {
        while (true)
        {
          SaslServer localSaslServer;
          do
          {
            if (i == 0);
            localSaslServer = localSaslServerFactory.createSaslServer(paramString1, paramString2, paramString3, paramMap, paramCallbackHandler);
          }
          while (localSaslServer == null);
          localObject = localSaslServer;
        }
        if (!(arrayOfString[j].equals(paramString1)))
          break;
        i = 1;
      }
      ++j;
    }
  }

  private static Collection<?> findFactories(String paramString)
  {
    HashSet localHashSet2;
    int i;
    String str2;
    HashSet localHashSet1 = new HashSet();
    Provider[] arrayOfProvider = Security.getProviders();
    if ((arrayOfProvider == null) || (arrayOfProvider.length == 0));
    do
    {
      return localHashSet1;
      localHashSet2 = new HashSet();
      i = 0;
    }
    while (i >= arrayOfProvider.length);
    String str1 = arrayOfProvider[i].getName();
    Enumeration localEnumeration = arrayOfProvider[i].keys();
    do
    {
      while (!(localEnumeration.hasMoreElements()))
        ++i;
      str2 = (String)localEnumeration.nextElement();
    }
    while (!(str2.startsWith(paramString)));
    String str3 = arrayOfProvider[i].getProperty(str2);
    try
    {
      if (!(localHashSet2.add(str1.concat(str3))));
      localHashSet1.add(newInstance(str3, arrayOfProvider[i]));
    }
    catch (SaslException localSaslException)
    {
      localSaslException.printStackTrace();
    }
  }

  public static Enumeration<SaslClientFactory> getSaslClientFactories()
  {
    return Collections.enumeration(findFactories("SaslClientFactory"));
  }

  public static Enumeration<SaslServerFactory> getSaslServerFactories()
  {
    return Collections.enumeration(findFactories("SaslServerFactory"));
  }

  private static Object newInstance(String paramString, Provider paramProvider)
    throws SaslException
  {
    ClassLoader localClassLoader = paramProvider.getClass().getClassLoader();
    if (localClassLoader == null)
      localClassLoader = ClassLoader.getSystemClassLoader();
    try
    {
      Object localObject = Class.forName(paramString, true, localClassLoader).newInstance();
      return localObject;
    }
    catch (IllegalAccessException localIllegalAccessException)
    {
      throw new SaslException("auth.31" + paramString, localIllegalAccessException);
    }
    catch (ClassNotFoundException localClassNotFoundException)
    {
      throw new SaslException("auth.31" + paramString, localClassNotFoundException);
    }
    catch (InstantiationException localInstantiationException)
    {
      throw new SaslException("auth.31" + paramString, localInstantiationException);
    }
  }
}